import Vue from 'vue'
import VueRouter from 'vue-router'
import message from '@mdsfe/mds-ui/dist/message'

import Main from '../views/main/index.vue'

Vue.use(VueRouter)

// NavigationDuplicated: Avoided redundant navigation to current location: "/user
const originalPush: any = VueRouter.prototype.push
VueRouter.prototype.push = function push(location: any) {
  return originalPush.call(this, location).catch((e: any) => e)
}

const routerReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location: any) {
  return routerReplace.call(this, location).catch((error: any) => error)
}

const routes: Array<any> = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    meta: { title: '首页' },
    component: Main
  },
  {
    path: '/notPermission',
    meta: { title: '暂无权限' },
    component: () => import('@/views/notPermission/index.vue')
  },
  {
    path: '/404',
    meta: { title: '404' },
    component: () => import('@/views/404.vue')
  },
  { path: '*', redirect: '/404' }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to: any, from: any, next: any) => {
  const metaTitlt = (to.meta && to.meta.title) || ''
  document.title = `${metaTitlt} - 默认模版`
  if (from.path !== '/' && from.matched[0].path !== to.matched[0].path) {
    message.destroy()
  }
  next()
})

export default router
